package ModelLayer;

import java.util.HashMap;
import java.util.Iterator;
/**
 * Uses a HashMap as a container for items. <String,item>.
 * 
 * @author  Group1
 * @version 2011-06-05
 */
public class ItemContainer
{
    private HashMap<String, Item> itemContainer; 
    private static ItemContainer _instance = null;
    
    /**
     * Creates a HashMap.(Container for items)
     */
    private ItemContainer()
    {
        itemContainer = new HashMap <String, Item>();
        addItemsToContainer();
    }
    
    public static ItemContainer getInstance()
    {
        if(null == _instance)
        {
            _instance = new ItemContainer();
        }
        return _instance;
    }
    
    /**
     * Returns the HashMap.
     * @return HashMap
     */
    public HashMap<String, Item> getItemContainer()
    {
        return itemContainer;
    }
    
    /**
     * Creates an item with a barcode as key and item as value and adds it to the HashMap.
     * @param barcode Barcode which belongs to the item
     * @param newItem item
     */
    public void createItem(String barcode, Item newItem)
    {
        itemContainer.put(barcode, newItem);
    }
    
    public void addItemsToContainer()
    {
        SupplierContainer supplierContainer = SupplierContainer.getInstance();
        Item newItem1 = new Item(10, 15, "2211", "Ring", 10, 0, "Byggecenter", "Stor ring. OE22mm", supplierContainer.findSupplierByName("NKT"), 100, 50, 200);
        Item newItem2 = new Item(50, 100,"3212", "Skruetraekker", 10, 0, "Traelast", "Stjerne", supplierContainer.findSupplierByName("Fisher"), 10, 5, 20);
        Item newItem3 = new Item(2500, 3000, "1232", "Plaeneklipper", 1, 0, "Byggecenter", "10 hk", supplierContainer.findSupplierByName("Dewalt"), 2, 1, 4);
        
        newItem1.setBarcode("123");
        newItem2.setBarcode("321");
        newItem3.setBarcode("999");
        
        itemContainer.put("123", newItem1);
        itemContainer.put("321", newItem2);
        itemContainer.put("999", newItem3);
    }
    
    /**
     * Find items by barcode.
     * @param searchBarcode the barcode you want to search for
     * @return item If barcode is found it returns the item, else return null
     */
    public Item findItemByBarcode(String searchBarcode)
    {
        Item item = itemContainer.get(searchBarcode);
        return item;
    }
    
    /**
     * Find item by a itemnumber.
     * @param searchItemNumber The item number of the item you want to search for
     * @return itemForSearch If item number is found, it returns an item, else returns null 
     */
    public Item findItemByItemNumber(String searchItemNumber)
    {
        Iterator it = itemContainer.keySet().iterator();
        boolean found = false;
        Item itemForSearch = null;
        while(it.hasNext() && !found)
        {
            Item item = itemContainer.get(it.next());
            if(item.getItemNumber().equals(searchItemNumber))
            {
                found = true;
                itemForSearch = item;
            }
        }
        return itemForSearch;
    }
    
    /**
     * Deletes an item from the HashMap.
     * @param barcode The barcode of the item you want to delete
     */
    public void deleteItem(String barcode)
    {
        itemContainer.remove(barcode);
    }
}